// 引入Vue
import Vue from 'vue'

// 引入Vue-router
import Router from 'vue-router'

// 使用路由插件
Vue.use(Router)

// 引入最外层骨架的一级路由组件：LayoutView
import LayoutView from '@/layout/LayoutView.vue'

/**
 * 注意：子菜单只在路由子菜单时出现。长度 >= 1。
 *
 * hidden: true                   如果设置为true，项目将不会显示在侧边栏中（默认为False）。
 * alwaysShow: true               如果设置为true，将始终显示根菜单。如果没有设置，当item有多个子路由时，它将变成嵌套模式，否则不显示根菜单。
 * redirect: noRedirect           如果设置为true，则不会在面包屑（breadcrumb）中重定向。
 * name:'router-name'             该名称由<keep-alive>使用，必须设置。
 * meta : {
 * roles: ['admin','editor']     控制页面角色（可以设置多个角色）。
 *  title: 'title'               名称显示在侧边栏和面包屑中（推荐设置）。
 *  icon: 'svg-name'/'el-icon-x' 图标显示在侧边栏中。
 *  breadcrumb: false            如果设置为false，则条目将隐藏在（breadcrumb）面包屑中，默认为true。
 *  activeMenu: '/example/list'  如果设置了路径，侧边栏将突出显示您设置的路径。
 * }
 * routes：                       没有权限要求的基页，所有角色都可以访问。
 */
export const routes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: LayoutView,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '控制面板', icon: 'dashboard' }
    }]
  },
  {
    path: '/example',
    component: LayoutView,
    redirect: '/example/table',
    name: 'Example',
    meta: { title: '模板管理', icon: 'template' },
    children: [
      {
        path: 'table',
        name: 'Table',
        component: () => import('@/views/table/index'),
        meta: { title: 'Table', icon: 'table' }
      },
      {
        path: 'tree',
        name: 'Tree',
        component: () => import('@/views/tree/index'),
        meta: { title: 'Tree', icon: 'tree' }
      }
    ]
  },
  {
    path: '/form',
    component: LayoutView,
    children: [
      {
        path: 'index',
        name: 'Form',
        component: () => import('@/views/form/index'),
        meta: { title: '表单管理', icon: 'form' }
      }
    ]
  },
  {
    path: '/product',
    name: 'Product',
    component: LayoutView,
    meta: { title: '商品管理', icon: 'goods' },
    children: [
      {
        path: 'trademark',
        name: 'Trademark',
        component: () => import('@/views/product/trademark/TrademarkView.vue'),
        meta: { title: '品牌管理', icon: 'el-icon-plus' }
      },
      {
        path: 'attribute',
        name: 'Attribute',
        component: () => import('@/views/product/attribute/AttributeView.vue'),
        meta: { title: '属性管理', icon: 'el-icon-minus' }
      },
      {
        path: 'spu',
        name: 'Spu',
        component: () => import('@/views/product/spu/SpuView.vue'),
        meta: { title: 'SPU管理', icon: 'el-icon-close' }
      },
      {
        path: 'sku',
        name: 'Sku',
        component: () => import('@/views/product/sku/SkuView.vue'),
        meta: { title: 'SKU管理', icon: 'el-icon-check' }
      }
    ]
  },
  {
    path: '/nested',
    component: LayoutView,
    redirect: '/nested/menu1',
    name: 'Nested',
    meta: { title: '嵌套管理', icon: 'nested' },
    children: [
      {
        path: 'menu1',
        component: () => import('@/views/nested/menu1/index'), // Parent router-view
        name: 'Menu1',
        meta: { title: 'Menu1', icon: 'el-icon-help' },
        children: [
          {
            path: 'menu1-1',
            component: () => import('@/views/nested/menu1/menu1-1'),
            name: 'Menu1-1',
            meta: { title: 'Menu1-1', icon: 'el-icon-help' }
          },
          {
            path: 'menu1-2',
            component: () => import('@/views/nested/menu1/menu1-2'),
            name: 'Menu1-2',
            meta: { title: 'Menu1-2', icon: 'el-icon-help' },
            children: [
              {
                path: 'menu1-2-1',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-1'),
                name: 'Menu1-2-1',
                meta: { title: 'Menu1-2-1', icon: 'el-icon-help' }
              },
              {
                path: 'menu1-2-2',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-2'),
                name: 'Menu1-2-2',
                meta: { title: 'Menu1-2-2', icon: 'el-icon-help' }
              }
            ]
          },
          {
            path: 'menu1-3',
            component: () => import('@/views/nested/menu1/menu1-3'),
            name: 'Menu1-3',
            meta: { title: 'Menu1-3', icon: 'el-icon-help' }
          }
        ]
      },
      {
        path: 'menu2',
        component: () => import('@/views/nested/menu2/index'),
        name: 'Menu2',
        meta: { title: 'menu2', icon: 'el-icon-help' }
      }
    ]
  },
  {
    path: '/iframe',
    name: 'IFrame',
    component: LayoutView,
    meta: { title: '内联框架', icon: 'el-icon-help' },
    children: [
      {
        path: 'baidu',
        name: 'BaiDu',
        component: () => import('@/views/iframe/baidu/index.vue'),
        meta: { title: '百度搜索', icon: 'form' }
      },
      {
        path: 'bing',
        name: 'Bing',
        component: () => import('@/views/iframe/bing/index.vue'),
        meta: { title: '必应搜索', icon: 'form' }
      },
      {
        path: 'cluster',
        name: 'Cluster',
        component: () => import('@/views/iframe/cluster/index.vue'),
        meta: { title: '集群管理', icon: 'form' }
      },
      {
        path: 'google',
        name: 'Google',
        component: () => import('@/views/iframe/google/index.vue'),
        meta: { title: '谷歌搜索', icon: 'form' }
      }
    ]
  },
  {
    path: '/echarts',
    name: 'ECharts',
    component: LayoutView,
    meta: { title: '数据可视化', icon: 'el-icon-help' },
    children: [
      {
        path: 'canvas',
        name: 'Canvas',
        component: () => import('@/views/echarts/canvas/index.vue'),
        meta: { title: '画布数据', icon: 'el-icon-help' }
      },
      {
        path: 'svg',
        name: 'Svg',
        component: () => import('@/views/echarts/svg/index.vue'),
        meta: { title: '向量图形', icon: 'el-icon-help' }
      }
    ]
  },
  {
    path: 'external-link',
    component: LayoutView,
    children: [
      {
        path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
        meta: { title: '联系我们', icon: 'link' }
      }
    ]
  },
  // 404 页面必须放在最后面。
  {
    path: '*',
    redirect: '/404',
    hidden: true
  }
]

const createRouter = () => new Router({
  mode: 'history',
  scrollBehavior: () => ({
    x: 0,
    y: 0
  }),
  routes: routes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
